package com.mission.game.creategame;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.mission.game.utils.Task;

public abstract class CreateAbstractTask extends Activity {

	protected Task _task;
	protected ArrayList<String> _taskList;
	protected EditText _taskNameView;
	protected EditText _taskDescriptionView;
	protected EditText _taskScoreView;

	protected static final String NAME_EXISTS = " as a task name already exists, please choose another.";
	protected static final String NAME_REQUIRED = "Task name is required, fill it in to continue.";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getContentView());
		_taskDescriptionView = (EditText) findViewById(getDescriptionInput());
		_taskNameView = (EditText) findViewById(getNameInput());
		_taskScoreView = (EditText) findViewById(getScoreInput());

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			if (extras.containsKey("task")) {
				_task = (Task) extras.getSerializable("task");
				setFields();
			} else {// Shouldn't get here, can't have a stand-alone task.
				_task = newSpecificTask();
			}
			if (extras.containsKey("tasklist")) {
				_taskList = extras.getStringArrayList("tasklist");
			}
		} else {
			_taskScoreView.setText(Integer.toString(Task.DEFAULT_SCORE));
		}
	}

	protected abstract int getScoreInput();

	protected void setFields() {
		_taskNameView.setText(_task.getName());
		_taskDescriptionView.setText(_task.getDescription());
		_taskScoreView.setText(Integer.toString(_task.getScore()));
	}

	protected abstract Task newSpecificTask();

	protected abstract int getDescriptionInput();

	protected abstract int getNameInput();

	protected abstract int getContentView();

	public void onBackToMissionClicked(View v) {
		String name = _taskNameView.getText().toString();
		String description = _taskDescriptionView.getText().toString();
		int score = Task.DEFAULT_SCORE;
		if (!_taskScoreView.getText().toString().isEmpty()) {
			score = Integer.parseInt(_taskScoreView.getText().toString());
		}
		if (name.isEmpty()) {
			Toast.makeText(getApplicationContext(), NAME_REQUIRED,
					Toast.LENGTH_LONG).show();
			return;
		} else if (!_task.getName().equals(name) && _taskList.contains(name)) {
			Toast.makeText(getApplicationContext(), name + NAME_EXISTS,
					Toast.LENGTH_LONG).show();
			return;
		}
		// Create a new task under the given mission.
		_task.setName(name);
		_task.setDescription(description);
		_task.setScore(score);
		specificBackToMissionClicked(v);
		getIntent().putExtra("task", _task);
		setResult(RESULT_OK, getIntent());
		finish();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
	    	setResult(Activity.RESULT_CANCELED);
	    }
	    return super.onKeyDown(keyCode, event);
	}

	protected abstract void specificBackToMissionClicked(View v);
}
